package components.scenes;

import java.util.ArrayList;
import java.util.List;

import maps.Addeable;

import com.uqbar.vainilla.GameScene;
import components.ChangingDirection;
import components.PacmanComponent;
import components.Walker;
import components.direction.Direction;
import components.ghost.Ghost;
import components.items.Food;
import components.items.SuperFood;
import components.pacman.Pacman;
import components.terrain.Path;
import components.terrain.Terrain;

public class MainGameScene extends ScoreableGameScene implements Addeable<PacmanComponent>{

	private List<Food> foodOnMap;
	private List<Ghost> ghosts;
	private MapMatrix mapMatrix;
	private Slot pacmanSpawn;
	private Slot ghostSpawn;

	public MainGameScene(){
		super(600, 3, 0);
		this.ghosts = new ArrayList<Ghost>();
		this.foodOnMap = new ArrayList<Food>();
		setMapMatrix(new MapMatrix(50, 50));
		this.setPacmanSpawn(new Slot(3,1));
		this.setGhostSpawn(new Slot(10,10));
	}
	
	
	
	public void addTerrain(Terrain terrain, int xslot, int yslot){
		
		this.getMapMatrix().setTerrain(xslot, yslot, terrain);
		this.addComponent(terrain);
	}
	public void addComp(PacmanComponent pc, int xslot, int yslot){
		
		this.getMapMatrix().centerComponentOnSlot(pc, xslot, yslot);
		this.addCollisionable(pc);
	}
	public void addWalker(Walker walker, int xslot, int yslot){
		this.respawnWalker(walker, new Slot(xslot, yslot));
		this.addCollisionable(walker);
	}

	public void addFood(Food food, int xslot, int yslot){
		this.getMapMatrix().centerComponentOnSlot(food, xslot, yslot);
		this.addTerrain(new Path(), xslot, yslot);
		this.increaseFood(food);
		this.addCollisionable(food);
	}

	public void addGhost(Ghost ghost, int xslot, int yslot){
		this.addWalker(ghost, xslot, yslot);
		this.ghosts.add(ghost);
	}
	
	public void addPacmanSpawn(int xslot, int yslot){
		this.addTerrain(new Path(), xslot, yslot);
		this.setPacmanSpawn(new Slot(xslot, yslot));
	}
	public void addGhostSpawn(int xslot, int yslot){
		this.addTerrain(new Path(), xslot, yslot);
		this.setGhostSpawn(new Slot(xslot, yslot));
	}
	
	private void increaseFood(Food food) {
		this.foodOnMap.add(food);
	}

	public void foodEaten(Food food){
		this.foodOnMap.remove(food);
		food.destroyComponent();
		this.addScore(100);
		if (this.foodOnMap.isEmpty()){
			this.gameOver();
		}
	}
	

	public void respawnWalker(Walker walker, Slot respawn){
		walker.setWalkerState(new ChangingDirection(walker));
		walker.setCurrentX(respawn.getX());
		walker.setCurrentY(respawn.getY());
		walker.setQueuedDir(Direction.EAST);
		walker.setDir(Direction.EAST);
		this.getMapMatrix().centerComponentOnSlot(walker, respawn.getX(), respawn.getY());
	}


	public MapMatrix getMapMatrix() {
		return mapMatrix;
	}


	public void setMapMatrix(MapMatrix mapMatrix) {
		this.mapMatrix = mapMatrix;
	}


	public Slot nextSlot(int x, int y,
			Direction dir) {
		return this.getMapMatrix().nextSlot(x, y, dir);
	}


	@Override
	public void addObject(PacmanComponent component, int x, int y) {
		component.addToScene(this, x, y);
	}


	@Override
	public GameScene getGameOverScene(int score) {
		return new EndScene(score, this.getGame().getDisplayWidth(), this.getGame().getDisplayHeight());
	}


	public Slot getGhostSpawn() {
		return ghostSpawn;
	}



	public void setGhostSpawn(Slot ghostSpawn) {
		this.ghostSpawn = ghostSpawn;
	}

	public Slot getPacmanSpawn() {
		return pacmanSpawn;
	}

	public void setPacmanSpawn(Slot pacmanSpawn) {
		this.pacmanSpawn = pacmanSpawn;
	}

	public void respawnPacman(Pacman pacman) {
		this.respawnWalker(pacman, this.getPacmanSpawn());
	}
	public void respawnGhost(Ghost ghost){
		this.respawnWalker(ghost, getGhostSpawn());
	}


	public void superfoodPickedup(SuperFood superFood) {
		this.addScore(300);
		this.foodEaten(superFood);
		this.transformGhosts();
	}

	private void transformGhosts() {
		for(Ghost ghost: this.ghosts){
			ghost.becomeEatable(5);
		}
	}



	public void pacmanDied(Pacman pacman) {
		this.loseLife();
		this.respawnPacman(pacman);
	}
	
}
